<template>
  <div class="container">
    <h3>{{ title }}</h3>
    <el-form
      ref="registerForm"
      :model="model"
      :rules="rules"
      label-width="80px"
    >
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="model.userName" clearable></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="model.password" clearable></el-input>
      </el-form-item>
      <el-form-item label-width="0px">
        <el-button @click="handleRegister" icon="el-icon-thumb">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { formValidateGen } from '@/utils/form-validator-gen'
export default {
  name: 'register',
  data () {
    return {
      model: {
        userName: '',
        password: ''
      },
      title: '注册',
      rules: {
        userName: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { validator: formValidateGen('validateName', '不合法的用户名'), trigger: 'change' }],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { validator: formValidateGen('validatePass', '长度不超过6位的合法字符'), trigger: 'change' }]
      }
    }
  },
  methods: {
    async handleRegister () {
      console.log('register')
      try {
        const res = await this.$api.authentication.register(this.model.userName, this.model.password)
        if (res && res.code === 0) {
          this.$message({
            type: 'success',
            message: '注册成功'
          })
        }
      } catch (e) {
        console.log(e)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  width: 50%;
  margin: 0 auto;
}
</style>
